<template>
  <div>
    <!-- el-form
      model 绑定数据对象
      rules 规则对象
    el-form-item
      prop 字段名 -->
    <el-form :model="formData" :rules="rules">
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="formData.checkPass" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const validCheckPass = (rule, val, callback) => {
      const isValid = val === this.formData.password
      if (isValid) {
        callback()
      } else {
        callback(new Error('两次输入不一样'))
      }
    }

    return {
      formData: {
        mobile: '13800000003',
        password: '123456'
      },
      rules: {
        // 校验规则名字跟数据对象一致, value 是个数组
        mobile: [
          // 数组当中可以放任意的规则对象, 包括 规则 触发器 报错语句
          // 最常见三钟, 必填, 长度范围, 正则
          { required: true, message: '手机不能为空', trigger: 'blur' },
          { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { max: 12, min: 6, message: '密码在 6-12 位之间', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validCheckPass, trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

<style>

</style>
